package cn.kgc.servlet;

import cn.kgc.pojo.News;
import cn.kgc.service.NewsService;
import cn.kgc.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeleteServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收新闻ID，并调用后台方法，根据ID将新闻信息从数据库中删除
        request.setCharacterEncoding("utf-8");
        News news = new News();
        news.setId(Integer.parseInt(request.getParameter("id")));
        String flag = "failed";

        NewsService newsService = new NewsServiceImpl();
        if (newsService.deleteNews(news)){
            flag = "success";
        }
        response.sendRedirect("/news/jsp/admin/newsDetailList.jsp?flag="+flag);
    }
}
